package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chentengfei
 * @desc 权限管理-角色管理接口
 * @date 2023-06-10 17:48:44
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/admin/acl/role")
// @CrossOrigin  //解决跨域
public class RoleController {

    @Autowired
    private RoleService roleService;

    /***
     * 角色列表（条件分页查询）
     * @author chentengfei
     * @date 2023/6/10 17:58
     * @param current
     * @param limit
     * @param roleQueryVo
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("角色条件分页查询")
    @GetMapping("{current}/{limit}")
    public Result pageList(@PathVariable Long current, @PathVariable Long limit, RoleQueryVo roleQueryVo){
        //1 创建page对象，传递当前页和每页记录数
        // current：当前页
        // limit: 每页显示记录数
        Page<Role> pageParam = new Page<>(current,limit);
        //2 调用service方法实现条件分页查询，返回分页对象
        IPage<Role> pageModel = roleService.selectRolePage(pageParam,roleQueryVo);
        return Result.ok(pageModel);
    }

    /***
     * 根据id查询角色
     * @author chentengfei
     * @date 2023/6/10 18:05
     * @param id
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("根据id查询角色")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id){
        Role byId = roleService.getById(id);
        return Result.ok(byId);
    }

    /***
     * 添加角色
     * @author chentengfei
     * @date 2023/6/10 18:08
     * @param role
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role){
        boolean is_success = roleService.save(role);
        if(is_success){
            return Result.ok(null);
        }else{
            return Result.fail(null);
        }
    }

    /***
     * 修改角色
     * @author chentengfei
     * @date 2023/6/10 18:09
     * @param role
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result update(@RequestBody Role role){
        roleService.updateById(role);
        return Result.ok(null);
    }

    /***
     * 根据id删除角色
     * @author chentengfei
     * @date 2023/6/10 18:09
     * @param id
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("根据id删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.ok(null);
    }

    /***
     * 批量删除角色
     * json数组[1,2,3]  --- java的list集合
     * @author chentengfei
     * @date 2023/6/10 18:09
     * @param idList
     * @return com.atguigu.ssyx.common.result.Result
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        roleService.removeByIds(idList);
        return Result.ok(null);
    }

}
